package com.rs.controller;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.github.pagehelper.Page;
import com.rs.model.CommonResult;
import com.rs.model.ZhClasses;
import com.rs.model.ZhUser;
import com.rs.service.PublicUserMethodsService;
import com.rs.service.ZhClassesService;
import com.rs.service.ZhLeanprogressService;
import com.rs.service.ZhUserStudentService;
import com.rs.util.DateFormatUtil;
import com.rs.util.SpringContextUtils;

import javax.swing.ListSelectionModel;
import javax.swing.JButton;
import javax.swing.JDesktopPane;

import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.Font;
@SuppressWarnings("all")
public class SelectClass extends JInternalFrame {
	
	
	JDesktopPane desktopPane_1;

	private JPanel contentPane;
	private JTable table;
	
	private ZhClassesService classesService = SpringContextUtils.getBean(ZhClassesService.class);
	private PublicUserMethodsService pum = SpringContextUtils.getBean(PublicUserMethodsService.class);
	private ZhUserStudentService zhUserService  = SpringContextUtils.getBean(ZhUserStudentService.class);
	private ZhLeanprogressService leanprogressService=SpringContextUtils.getBean(ZhLeanprogressService.class);
	private DateFormatUtil df=SpringContextUtils.getBean(DateFormatUtil.class);
	
	int pageSum=1;
	int pages;
	
	int classState =0;  //设置状态 0是未毕业班级 1是毕业班级
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectClass frame = new SelectClass();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SelectClass() {
		aa();
	}
	public SelectClass( JDesktopPane desktopPane_1) {
		this.desktopPane_1=desktopPane_1;
		aa();
	}
	
	void aa(){
		setClosable(true);
		setIconifiable(true);
		setMaximizable(true);
		setBounds(100, 100, 683, 505);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 89, 607, 276);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowHeight(25);
		table.getModel();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
				{null, null, null, null, null, null, null},
			},
			new String[] {
				"id", "\u6559\u5E08", "\u73ED\u7EA7\u540D", "人数", "学习进度", "开学时间", "结业时间"
			}
		));
		scrollPane.setViewportView(table);
		
		JButton btn_upPage = new JButton("上一页");
		btn_upPage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				upPage();
			}
		});
		btn_upPage.setBounds(421, 390, 93, 23);
		contentPane.add(btn_upPage);
		
		JButton btn_nextPage = new JButton("下一页");
		btn_nextPage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nextPage();
			}
		});
		btn_nextPage.setBounds(524, 390, 93, 23);
		contentPane.add(btn_nextPage);
		
		JButton button = new JButton("毕业班级");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageSum=1;
				classState=1;
				aa();
			}
		});
		button.setBounds(10, 45, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("未毕业班级");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				classState=0;
				pageSum=1;
				aa();
			}
		});
		button_1.setBounds(126, 45, 93, 23);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("查看班级学员及教师");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int clid = getRowId();
				SelectUser2 users = new SelectUser2(0,clid,desktopPane_1);
				users.setVisible(true);
			}
		});
		button_2.setBounds(10, 420, 148, 28);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("修改");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {	
				new ClassInfo(desktopPane_1,getRowId(),2).setVisible(true);
			}
		});
		button_3.setBounds(10, 387, 70, 28);
		contentPane.add(button_3);
		
		JButton button_4 = new JButton("删除");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				delClass();
			}
		});
		button_4.setBounds(90, 387, 70, 28);
		contentPane.add(button_4);
		
		JLabel label = new JLabel("班级列表");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("宋体", Font.PLAIN, 16));
		label.setBounds(290, 20, 84, 23);
		contentPane.add(label);
		
		nextPage();
	}
	

	public void nextPage(){
		List<ZhClasses> cla = getClasses();
		if( pageSum >= pages && pageSum !=1){
			JOptionPane.showMessageDialog(this, "当前已是最后一页！");
			return;
		}
		setTheValue(cla);
		pageSum++;	
	}
	
	
	
	public void upPage(){
		if(pageSum<=2){
			JOptionPane.showMessageDialog(this, "当前是第一页！");
			return;
		}
		List<ZhClasses> cla = getClasses();
		setTheValue(cla);
		pageSum--;
	}
	
	
	public void setTheValue(List<ZhClasses> cla){
		int sum =0;
		TableModel tb=table.getModel();
		listNull(tb,tb.getRowCount(),tb.getColumnCount());
		for(ZhClasses cc :cla){
			tb.setValueAt(cc.getClId(),sum , 0);
			tb.setValueAt(getTeacherName(cc.getClUid()),sum , 1);
			tb.setValueAt(cc.getClName(),sum , 2);
			tb.setValueAt(getStudents(cc.getClId()),sum , 3);
			if(cc.getClLpgid()!=null){
				tb.setValueAt(getLpgJd(cc.getClLpgid())+"%",sum , 4);
			}
			tb.setValueAt(df.getDateString(cc.getClTime(), "yyyy-MM-dd"),sum , 5);
			tb.setValueAt(df.getDateString(cc.getClEndtime(), "yyyy-MM-dd"),sum , 6);
			sum++;
		}
		
	}
	
	//得到选中的行 的id
	private Integer getRowId(){
		int checkedRow=table.getSelectedRow();
		return (Integer)table.getModel().getValueAt(checkedRow, 0);
	}
	
	private List<ZhClasses> getClasses(){
		if(classState==1){
			 Page<ZhClasses> cla=classesService.graduationZhClassess(pageSum);
			 pages=cla.getPages();
			return cla.getResult();
		}else{
			Page<ZhClasses> cla=classesService.notGraduateZhClassess(pageSum);
			 pages=cla.getPages();
			return cla.getResult();
		}
	}
	
	//展示所有已毕业班级
	private List<ZhClasses> graduationZhClassess(){
		return classesService.graduationZhClassess(pageSum);
	}
	//。展示所有未毕业班级
	private List<ZhClasses> notGraduateZhClassess(){
		return classesService.notGraduateZhClassess(pageSum);
	}
	
	public void listNull(TableModel tb,int row,int cou){
		 for(int i=0;i<row;i++){
			for(int j=0;j<cou;j++){
				tb.setValueAt(null, i, j);
			}
		 }
	}
	
	//得到教师名字
	private String getTeacherName(Integer uid){
		return pum.selectUserId(uid).getuName();
	}
	
	//得到班级人
	private Integer getStudents(Integer caid){
		ZhUser user = new ZhUser();
		user.setPageNum(pageSum);
		user.setuCaid(caid);
		List<ZhUser> list = zhUserService.selectConditionStudents(user);
		return list.size();
	}
	
	private void alert(String aa){
		JOptionPane.showMessageDialog(this, aa);
	}
	
	//删除某条信息
	private void delClass(){
		int id=getRowId();
		CommonResult<Integer> a=classesService.deleteByPrimaryKey(id);
		alert(a.getData()>0?"删除成功":"删除失败");
		DefaultTableModel tt = (DefaultTableModel) table.getModel();
		int checkedRow=table.getSelectedRow();
		tt.removeRow(checkedRow);
		tt.insertRow(tt.getRowCount(), new String[]{null, null, null, null, null, null, null});
	}
	
	private Integer getLpgJd(Integer lpgId){
		return leanprogressService.selectZhLean(lpgId).getLpgJd();
	}
	
}


